package com.atguigu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/community")
public class CommunityController extends BaseController{
    @Reference
    private CommunityService communityService;
    @Reference
    private DictService dictService;
    @RequestMapping
    public String index(ModelMap model, HttpServletRequest request) {
        // 对查询条件和分页参数进行封装
        Map<String,Object> filters = getFilters(request);
        model.addAttribute("filters", filters);
        // 访问业务层获取符合条件的分页数据
        PageInfo<Community> page = communityService.findPage(filters);
        model.addAttribute("page", page);
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        model.addAttribute("areaList",areaList);
        return "community/index";
    }
    @RequestMapping("/create")
    public String create( Model model){
        //获取区域列表areaList
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        model.addAttribute("areaList",areaList);
        return "community/create";
    }
    @RequestMapping("/save")
    public String save(Community community){
        communityService.insert(community);
        return "common/successPage";
    }
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id,Model model){
        //带指定id小区的信息
        Community community = communityService.getById(id);
        model.addAttribute("community",community);
        //带所有区域列表
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        model.addAttribute("areaList",areaList);
        return "community/edit";
    }

    @RequestMapping("/update")
    public String update(Community community){
        communityService.update(community);
        return "common/successPage";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        communityService.delete(id);
        return "redirect:/community";
    }
}
